package com.vitalfit.strategy.strategyImpl;

import com.vitalfit.handler.HandlerChainManager;
import com.vitalfit.handler.handlerImpl.TrainerHandler;
import com.vitalfit.model.dto.HandleDTO;
import com.vitalfit.model.entity.user.Trainer;
import com.vitalfit.repository.UserRepository;
import com.vitalfit.strategy.UserStrategy;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CreateTrainerStrategy implements UserStrategy {
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    @Override
    @Transactional
    public Object execute(HandleDTO handleDTO) {
        Trainer trainer = new Trainer();

        HandlerChainManager<Trainer> handlerChainManager = new HandlerChainManager<>();
        handlerChainManager.addHandler(new TrainerHandler(passwordEncoder));
        handlerChainManager.handle(trainer, handleDTO);

        return userRepository.save(trainer);
    }
}

